package com.example.taobaounion3.ui.fragment;

import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.taobaounion3.R;
import com.example.taobaounion3.base.baseFragment;
import com.example.taobaounion3.model.domain.SelectedContent;
import com.example.taobaounion3.model.domain.SelectedPageCategory;
import com.example.taobaounion3.presenter.ISelectedPagePresenter;
import com.example.taobaounion3.presenter.ITicketPresenter;
import com.example.taobaounion3.ui.activity.TicketActivity;
import com.example.taobaounion3.ui.adapter.SelectedPageContentAdapter;
import com.example.taobaounion3.ui.adapter.SelectedPageLeftAdapter;
import com.example.taobaounion3.utils.LogUtils;
import com.example.taobaounion3.utils.PresenterManager;
import com.example.taobaounion3.utils.SizeUtils;
import com.example.taobaounion3.view.ISelectedPageCallback;

import java.util.List;

import butterknife.BindView;

public class SelectedFragment extends baseFragment implements ISelectedPageCallback, SelectedPageLeftAdapter.OnLeftItemClickListener, SelectedPageContentAdapter.OnSelectedPageContentItemClickListener {

    private ISelectedPagePresenter mSelectedPagePresenter;
    private SelectedPageContentAdapter mRightAdapter;
    private SelectedPageLeftAdapter mLeftAdapter;

    @BindView(R.id.left_category_list)
    public RecyclerView leftCategoryList;

    @BindView(R.id.right_content_list)
    public RecyclerView rightCategoryList;


    @Override
    protected int getRootViewResId() {
        return R.layout.fragment_selected;
    }

    @Override
    protected View loadRootView(LayoutInflater inflater, ViewGroup container) {
        return inflater.inflate(R.layout.fragment_with_bar_layout,container,false);
    }

    @Override
    protected void initView(View rootView) {
        setUpState(State.SUCCESS);
        //设置分类适配器
        leftCategoryList.setLayoutManager(new LinearLayoutManager(getContext()));
        mLeftAdapter=new SelectedPageLeftAdapter();
        leftCategoryList.setAdapter(mLeftAdapter);
        //设置内容适配器
        rightCategoryList.setLayoutManager(new LinearLayoutManager(getContext()));
        mRightAdapter=new SelectedPageContentAdapter();
        //设置RecycleView的item的外边距
        rightCategoryList.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
                super.getItemOffsets(outRect, view, parent, state);
                int topAndBottom= SizeUtils.dip2px(getContext(),4);
                int leftAndRight= SizeUtils.dip2px(getContext(),6);
                outRect.left=leftAndRight;
                outRect.right=leftAndRight;
                outRect.top=topAndBottom;
                outRect.bottom=topAndBottom ;
            }
        });
        //设置CardView的相关属性

        rightCategoryList.setAdapter(mRightAdapter);
    }

    @Override
    protected void initpresenter() {
        super.initpresenter();
        mSelectedPagePresenter= PresenterManager.getInstance().getmSelectedPagePresenter();
        mSelectedPagePresenter.registerViewCallback(this);
        mSelectedPagePresenter.getSelectedCategories();
    }

    @Override
    protected void release() {
        super.release();
        if (mSelectedPagePresenter!=null) {
            mSelectedPagePresenter.unregisterViewCallback(this);
        }

    }

    @Override
    protected void initListener() {
        super.initListener();
        mLeftAdapter.setOnLeftItemClickListener(this);
        mRightAdapter.setOnSelectedPageContentItemClickListener(this);
    }

    @Override
    public void onCategoriesLoaded(SelectedPageCategory categories) {
        setUpState(State.SUCCESS);
        mLeftAdapter.setData(categories);
//        //分类内容从这里回来
////        LogUtils.d(this,"Selected onCategoriesLoaded-->"+categories);
////        //TODO:更新UI
////        //根据当前选择的分类获取内容
////        List<SelectedPageCategory.DataBean> data = categories.getData();
////        mSelectedPagePresenter.getSelectedContentByCategory(data.get(0));

    }

    @Override
    public void onContentLoaded(SelectedContent content) {
        mRightAdapter.setData(content);
        rightCategoryList.scrollToPosition(0);
    }

    @Override
    protected void onRetryClick() {
        //重试
        if (mSelectedPagePresenter!=null) {
            mSelectedPagePresenter.reloadContent();
        }
    }

    @Override
    public void onError() {
        setUpState(State.ERROR);
    }

    @Override
    public void onLoading() {
        setUpState(State.LOADING);
    }

    @Override
    public void onEmpty() {

    }

    @Override
    public void onLeftItemClick(SelectedPageCategory.DataBean item) {
        //左边的分类点击了
        mSelectedPagePresenter.getSelectedContentByCategory(item);
        LogUtils.d(this,"current selected item"+item.getFavorites_title());
    }

    @Override
    public void onContentItemClick(SelectedContent.DataBean.TbkDgOptimusMaterialResponseBean.ResultListBean.MapDataBean item) {
        //处理数据
        //拿到ticketPresenter去加载数据
        String title =item.getTitle();
        String url =item.getCoupon_click_url();
        if (TextUtils.isEmpty(url)){
            url =item.getClick_url();
        }
        //图片的URL
        String cover=item.getPict_url();
        LogUtils.d(this,"SelectedContent cover-->"+cover);
        ITicketPresenter ticketPresenter=PresenterManager.getInstance().getmTicketPresenter();
        ticketPresenter.getTicket(title,url,cover);
        startActivity(new Intent(getContext(), TicketActivity.class));
    }
}
